﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.SessionState;
using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string username = context.Request["username"];
                string password = context.Request["password"];
                string keepalive = context.Request["keepalive"];

                User_BLL bll = new User_BLL();
                USER user = bll.GetModelBySQL(string.Format("select USER_ID,USER_NAME,USER_NIKENAME,PASSWORD,EMAIL,TELEPHONE,ENABLE,USER_HEAD_IMG from [USER] where USER_NAME='{0}'", username));
                if (user == null)
                {
                    info.Success = false;
                    info.Tip = "帐号错误";
                }
                else if (Tom.Net.Security.EncryptUtil.EncryptDES(password, "Omabar") == user.PASSWORD)
                {
                    info.Success = true;
                    info.Tip = "登陆成功";
                    info.Url = "Index.aspx";
                    context.Session["CURRENTUSER"] = user;
                }
                else
                {
                    info.Success = false;
                    info.Tip = "密码错误";
                }

            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("登陆失败", ex);
                info.Success = false;
                info.Tip = "登陆失败";
                info.Url = "Index.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}